package com.ymk.base.utlis;

import cn.hutool.core.util.ObjectUtil;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by laborc on 2018/4/26.
 */
public class DateUtil {
    public DateUtil() {
    }

    public static Date stringToDate(String dateStr, String formatStr) {
        if (dateStr != null && !"".equals(dateStr)) {
            DateFormat format = new SimpleDateFormat(formatStr);
            //format.setTimeZone(TimeZone.getTimeZone("GMT"));
            Date date = null;

            try {
                date = format.parse(dateStr);
            } catch (ParseException var5) {
                var5.printStackTrace();
            }

            return date;
        } else {
            return null;
        }
    }

    public static String dateToString(Date date, String formatStr) {
        if (date == null) {
            return null;
        } else {
            try {
                DateFormat format = new SimpleDateFormat(formatStr);
                return format.format(date);
            } catch (Exception var3) {
                var3.getMessage();
                return null;
            }
        }
    }

    public static String getDateUUid() {
        SimpleDateFormat format = new SimpleDateFormat("yyMMddHHmmss");

        try {
            return format.format(new Date()) + getRandomInt();
        } catch (Exception var2) {
            var2.getMessage();
            return null;
        }
    }

    public static String getDateUUid(String prefix) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");

        try {
            return prefix + format.format(new Date()) + getRandomInt();
        } catch (Exception var2) {
            var2.getMessage();
            return null;
        }
    }

    public static int getRandomInt() {
        return (int) (Math.floor(Math.random() * (90)) + 10);
    }

    public static double getRandomInt(Long limit) {
        return (Math.floor(Math.random() * (limit)) + 1000000000);
    }

    public static String dateAdd(String baseDate, int addNum) throws ParseException {
        if (baseDate != null && !"".equals(baseDate)) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date dt = sdf.parse(baseDate);
            Calendar cal = Calendar.getInstance();
            cal.setTime(dt);
            cal.add(5, 1);
            return sdf.format(cal.getTime());
        } else {
            return null;
        }
    }

    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime   当前时间
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @return
     * @author jqlin
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 时间加分钟
     *
     * @param nowDate
     * @param count
     * @return
     */

    public static Date dateAddMinutes(Date nowDate, int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.MINUTE, count);
        return calendar.getTime();
    }

    public static String getMonth(Date date) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH);
        switch (month) {
            case 0:
                return "一月";

            case 1:
                return "二月";

            case 2:
                return "三月";

            case 3:
                return "四月";

            case 4:
                return "五月";

            case 5:
                return "六月";

            case 6:
                return "七月";

            case 7:
                return "八月";

            case 8:
                return "九月";

            case 9:
                return "十月";

            case 10:
                return "十一月";

            case 11:
                return "十二月";

            default:
                return "";
        }
    }

    /**
     * 加月份
     *
     * @param nowDate 当前时间
     * @return 月份
     */
    public static Date dateAddMonth(Date nowDate, int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.MONTH, count);
        return calendar.getTime();
    }

    /**
     * 加日
     *
     * @param nowDate 当前时间
     * @return 月份
     */
    public static Date dateAddDay(Date nowDate, int count) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(nowDate);
        calendar.add(Calendar.DATE, count);
        return calendar.getTime();
    }

    /**
     * 当前天开始
     *
     * @param nowDate
     * @return
     */
    public static Date dayStart(Date nowDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 00:00:01");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.parse(format.format(nowDate));
    }

    /**
     * 当前天结束
     *
     * @param nowDate
     * @return
     */
    public static Date dayEnd(Date nowDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.parse(format.format(nowDate));
    }


    /**
     * 当前月份第一天
     *
     * @param nowDate
     * @return
     */
    public static Date firstDay(Date nowDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-01 00:00:00");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.parse(format.format(nowDate));
    }

    /**
     * 当前月份最后一天
     *
     * @param nowDate
     * @return
     */
    public static Date lastDay(Date nowDate) throws ParseException {
        Calendar cal = Calendar.getInstance();
        cal.setTime(nowDate);
        int maxDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-" + maxDay + " 24:00:00");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.parse(format.format(cal.getTime()));
    }

    /**
     * 昨天的当前时间
     *
     * @param nowDate
     * @return
     */
    public static String Yesterday(Date nowDate) throws ParseException {
        Calendar calendar = Calendar.getInstance();
        //得到制前一天zd
        calendar.add(Calendar.DATE, -1);
        Date date = calendar.getTime();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(date);
    }

    /**
     * 根据增加的日期返回增加日期的长度
     *
     * @param startTime
     * @param endTime
     * @param dayNum
     * @return
     */
    public static List<Date> dateListAddDay(Date startTime, Date endTime, int dayNum) {
        List<Date> dates = new ArrayList<>();
        dates.add(startTime);
        while (startTime.getTime() <= endTime.getTime()) {
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startTime);
            calendar.add(Calendar.DATE, dayNum);
            startTime = calendar.getTime();
            if (calendar.getTime().getTime() <= endTime.getTime()) {
                dates.add(calendar.getTime());
            }

        }
        return dates;
    }

    /**
     * 获取时间之间的间隔
     *
     * @param startTime
     * @param endTime
     * @param interval  分钟
     * @return
     */
    public static List<Date> dateListAddMinute(Date startTime, Date endTime, int interval) {
        List<Date> dateList = new ArrayList<>();
        while (startTime.getTime() <= endTime.getTime()) {
            dateList.add(startTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startTime);
            calendar.add(Calendar.MINUTE, interval);

            if (calendar.getTime().getTime() > endTime.getTime()) {
                if (!startTime.equals(endTime)) {
                    dateList.add(endTime);
                }
                startTime = calendar.getTime();
            } else {
                startTime = calendar.getTime();
            }
        }

        return dateList;
    }

    /**
     * 是否是同一个月的
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isTheSameMonth(Date startTime, Date endTime) {
        if (ObjectUtil.isEmpty(startTime) || ObjectUtil.isEmpty(endTime)) {
            return false;
        }

        Calendar start = Calendar.getInstance();
        Calendar end = Calendar.getInstance();

        start.setTime(startTime);
        end.setTime(endTime);

        return start.get(Calendar.MONTH) == end.get(Calendar.MONTH);
    }

    /**
     * 获取时间之间的间隔
     *
     * @param startTime
     * @param endTime
     * @param interval  分钟
     * @return
     */
    public static List<Date> dateListAddMonth(Date startTime, Date endTime, int interval) {
        List<Date> dateList = new ArrayList<>();
        while (startTime.getTime() <= endTime.getTime()) {
            dateList.add(startTime);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(startTime);
            calendar.add(Calendar.MONTH, interval);

            if (calendar.getTime().getTime() > endTime.getTime()) {
                if (!startTime.equals(endTime) && !isTheSameMonth(startTime, endTime)) {
                    dateList.add(endTime);
                }
                startTime = calendar.getTime();
            } else {
                startTime = calendar.getTime();
            }
        }

        return dateList;
    }
    /**
     * 时间比较大小
     */
    public static Integer dateDifference(Date nowDate,Date indexDate){
        long nowDateTime = nowDate.getTime();
        long indexDateTime = indexDate.getTime();
        return (int)((nowDateTime - indexDateTime) / (1000 * 60 * 60 * 24));
    }


}
